//
//  FactoryResetViewController.swift
//  CheckmeRing
//
//  Created by 董鹏 on 2022/2/19.
//

import UIKit
import VTO2Lib
import SVProgressHUD

class FactoryResetViewController: RViewController, UIGestureRecognizerDelegate, VTO2CommunicateDelegate, VTBLEUtilsDelegate {

    
    @IBOutlet weak var confirmView: UIView!
    @IBOutlet weak var cancelBtn: UIButton!
    @IBOutlet weak var confirmBtn: UIButton!
    
    var blackBg = UIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = hexColor("F9F9F9")
        self.cancelBtn.layer.cornerRadius = 22
        self.confirmBtn.layer.cornerRadius = 22
        self.confirmBtn.layer.masksToBounds = true
        self.cancelBtn.layer.borderWidth = 1
        self.cancelBtn.layer.borderColor = hexColor("FF6054")?.cgColor
        
        let bgLayer1 = CAGradientLayer()
        bgLayer1.colors = [UIColor(red: 1, green: 0.49, blue: 0.4, alpha: 1).cgColor, UIColor(red: 1, green: 0.33, blue: 0.3, alpha: 1).cgColor]
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = self.confirmBtn.bounds
        bgLayer1.startPoint = CGPoint(x: 1.03, y: 0.67)
        bgLayer1.endPoint = CGPoint(x: 0.67, y: 0.67)
        self.confirmBtn.layer.insertSublayer(bgLayer1, at: 0)
    }

    /// 恢复出厂设置按钮
    @IBAction func resetBtnDidClicked(_ sender: Any) {
        self.confirmView.isHidden = false
        blackBg = UIView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: (self.navigationController?.navigationBar.height)! + getStatusBarHight() + 1))
        blackBg.backgroundColor = hexColor("000000", alpha: 0.7)
        let window = UIApplication.shared.keyWindow
        window?.addSubview(blackBg)
        self.view.bringSubviewToFront(self.confirmBtn)
    }
    
    @IBAction func cancelBtnDidClicked(_ sender: Any) {
        self.confirmView.isHidden = true
        blackBg.removeFromSuperview()
    }
    
    @IBAction func confirmBtnDidClicked(_ sender: Any) {
        
        if RManager.manager.currentDevice != nil {
            
            SVProgressHUD.show()
            VTBLEUtils.sharedInstance().delegate = self
            VTO2Communicate.sharedInstance().delegate = self
            VTO2Communicate.sharedInstance().beginFactory()
            
        }else {
            blackBg.removeFromSuperview()
            self.navigationController?.popViewController(animated: true)
        }
        
    }
    
    func commonResponse(_ cmdType: VTCmdType, andResult result: VTCommonResult) {
        VTO2Communicate.sharedInstance().beginGetInfo()
    }
    
    func didDisconnectedDevice(_ device: VTDevice, andError error: Error) {
        VTBLEUtils.sharedInstance().startScan()
    }

    func didDiscover(_ device: VTDevice) {
        let identifier = device.rawPeripheral.identifier.uuidString
        if identifier == RManager.manager.currentDevice?.rawPeripheral.identifier.uuidString {
            VTBLEUtils.sharedInstance().stopScan()
            VTBLEUtils.sharedInstance().connect(to: device)
        }
    }

    func didConnectedDevice(_ device: VTDevice) {
        VTO2Communicate.sharedInstance().peripheral = device.rawPeripheral
        VTO2Communicate.sharedInstance().delegate = self
    }

    func serviceDeployed(_ completed: Bool) {
        if completed {
            VTO2Communicate.sharedInstance().beginGetInfo()
        }else {
            SVProgressHUD.dismiss()
            blackBg.removeFromSuperview()
            self.navigationController?.popViewController(animated: true)
        }
    }
    
    func getInfoWithResultData(_ infoData: Data?) {
        guard let infoData = infoData else { return }
        RManager.manager.info = VTO2Parser.parseO2Info(with: infoData)
        SVProgressHUD.dismiss()
        blackBg.removeFromSuperview()
        self.navigationController?.popViewController(animated: true)
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        SVProgressHUD.dismiss()
        self.blackBg.removeFromSuperview()
    }
    
}
